package pl.edu.agh.student.janikhankus.earley;

/**
 * Class representing tuple of parse precedents. It is used to create parse tree
 * after word parsing.
 * 
 * @author tom
 * 
 */
public class Precedents implements Comparable<Precedents> {
	ParseProduction predecessor;
	ParseProduction reduction;

	public Precedents(ParseProduction predecessor, ParseProduction reduction) {
		this.predecessor = predecessor;
		this.reduction = reduction;

	}

	ParseProduction getPredecessor() {
		return predecessor;
	}

	ParseProduction getReduction() {
		return reduction;
	}

	@Override
	public int compareTo(Precedents p) {
		if (p == null) {
			return 1;
		}
		if (this == p) {
			return 0;
		}
		if (this.predecessor == null || p.getPredecessor() == null) {
			if (this.predecessor != p.getPredecessor()) {
				return 1;
			}
		} else {
			if (!this.predecessor.equals(p.getPredecessor())) {
				return 1;
			}
		}
		if (this.reduction == null || p.getReduction() == null) {
			if (this.reduction != p.getReduction()) {
				return 1;
			}
		} else {
			if (!this.reduction.equals(p.getReduction())) {
				return 1;
			}
		}
		return 0;
	}

	@Override
	public boolean equals(Object o) {

		if (o == null || !(o instanceof Precedents)) {
			return false;
		}
		if (this == o) {
			return true;
		}
		Precedents p = (Precedents) o;
		if (this.predecessor == null || p.getPredecessor() == null) {
			if (this.predecessor != p.getPredecessor()) {
				return false;
			}
		} else {
			if (!this.predecessor.equals(p.getPredecessor())) {
				return false;
			}
		}
		if (this.reduction == null || p.getReduction() == null) {
			if (this.reduction != p.getReduction()) {
				return false;
			}
		} else {
			if (!this.reduction.equals(p.getReduction())) {
				return false;
			}
		}
		return true;
	}

	@Override
	public int hashCode() {
		return (new Integer(4)).hashCode();
	}
}
